package com.cfr.utils;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.restlet.data.MediaType;
import org.restlet.ext.xml.DomRepresentation;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class DomRepresentationUtil {
    protected static final Logger logger = Logger.getLogger(DomRepresentationUtil.class.getClass());
    
	public static final String ROOT_TAG="Root";
	public static final String INFO_TAG="Info";
	public static final String DATA_ROOT_TAG="Datas";
	public static final String DATA_TAG="Data";
	public static final String CODE_TAG="code";
	public static final String RESPONSE_TIME_TAG="responseTime";
	public static final String COMMENT_TAG="comment";
	public static final String TOTAL_SIZE_TAG="TotalSize";
	
	
	public static DomRepresentation getDomRepresentation(String code, long responseTime, 
			String comment) {
		DomRepresentation xmlRep = null;
		try {
			xmlRep = new DomRepresentation(MediaType.APPLICATION_XML);
			Document doc = xmlRep.getDocument();
			Element rootElement = doc.createElement(DomRepresentationUtil.ROOT_TAG);
			Element infoElement = doc.createElement(DomRepresentationUtil.INFO_TAG);
			addElement(doc, infoElement, DomRepresentationUtil.CODE_TAG, code);
			addElement(doc, infoElement, DomRepresentationUtil.RESPONSE_TIME_TAG, String.valueOf(responseTime));
			addElement(doc, infoElement, DomRepresentationUtil.COMMENT_TAG, comment);
			rootElement.appendChild(infoElement);
			doc.appendChild(rootElement);
		} catch (DOMException e) {
			logger.error(e.getMessage(),e);
		} catch (IOException e) {
		}
		return xmlRep;
	}
	
	public static DomRepresentation getDomRepresentation(String code, long responseTime, 
			String comment, String attrName, String[] values) {
		DomRepresentation xmlRep = null;
		try {
			xmlRep = new DomRepresentation(MediaType.APPLICATION_XML);
			Document doc = xmlRep.getDocument();
			Element rootElement = doc.createElement(DomRepresentationUtil.ROOT_TAG);
			Element datasElement = doc.createElement(DomRepresentationUtil.DATA_ROOT_TAG);
			if(values!=null){
				Element dataElement = doc.createElement(DomRepresentationUtil.DATA_TAG);
				for(int i=0;i<values.length;i++){
					addElement(doc, dataElement, attrName, values[i]);
				}
				datasElement.appendChild(dataElement);
			}
			rootElement.appendChild(datasElement);
			addElement(doc, rootElement, DomRepresentationUtil.CODE_TAG, code);
			addElement(doc, rootElement, DomRepresentationUtil.RESPONSE_TIME_TAG, String.valueOf(responseTime));
			addElement(doc, rootElement, DomRepresentationUtil.COMMENT_TAG, comment);
			doc.appendChild(rootElement);
		} catch (DOMException e) {
			logger.error(e.getMessage(),e);
		} catch (IOException e) {
			logger.error(e.getMessage(),e);
		}
		return xmlRep;
	}
	
	public static DomRepresentation getDomRepresentation(String code, long responseTime, 
			String comment, String[] attrNames, String[][] values) {
		DomRepresentation xmlRep = null;
		try {
			xmlRep = new DomRepresentation(MediaType.APPLICATION_XML);
			Document doc = xmlRep.getDocument();
			Element rootElement = doc.createElement(DomRepresentationUtil.ROOT_TAG);
			Element datasElement = doc.createElement(DomRepresentationUtil.DATA_ROOT_TAG);
			if(attrNames!=null&&values!=null){
				for(int i=0;i<values.length;i++){
					Element dataElement = doc.createElement(DomRepresentationUtil.DATA_TAG);
					for(int j=0;j<values[i].length;j++){
						addElement(doc, dataElement, attrNames[j], values[i][j]);
					}
					datasElement.appendChild(dataElement);
				}
			}
			rootElement.appendChild(datasElement);
			addElement(doc, rootElement, DomRepresentationUtil.CODE_TAG, code);
			addElement(doc, rootElement, DomRepresentationUtil.RESPONSE_TIME_TAG, String.valueOf(responseTime));
			addElement(doc, rootElement, DomRepresentationUtil.COMMENT_TAG, comment);
			doc.appendChild(rootElement);
		} catch (DOMException e) {
			logger.error(e.getMessage(),e);
		} catch (IOException e) {
			logger.error(e.getMessage(),e);
		}
		return xmlRep;
	}
	
	public static DomRepresentation getDomRepresentation(String code, long responseTime, 
			String comment, String[] attrNames, List<String[]> values) {
		DomRepresentation xmlRep = null;
		try {
			xmlRep = new DomRepresentation(MediaType.APPLICATION_XML);
			Document doc = xmlRep.getDocument();
			Element rootElement = doc.createElement(DomRepresentationUtil.ROOT_TAG);
			Element datasElement = doc.createElement(DomRepresentationUtil.DATA_ROOT_TAG);
			if(attrNames!=null&&values!=null){
				for(int i=0;i<values.size();i++){
					Element dataElement = doc.createElement(DomRepresentationUtil.DATA_TAG);
					String[] data=values.get(i);
					for(int j=0;j<data.length;j++){
						addElement(doc, dataElement, attrNames[j], data[j]);
					}
					datasElement.appendChild(dataElement);
				}
			}
			rootElement.appendChild(datasElement);
			addElement(doc, rootElement, DomRepresentationUtil.CODE_TAG, code);
			addElement(doc, rootElement, DomRepresentationUtil.RESPONSE_TIME_TAG, String.valueOf(responseTime));
			addElement(doc, rootElement, DomRepresentationUtil.COMMENT_TAG, comment);
			doc.appendChild(rootElement);
		} catch (DOMException e) {
			logger.error(e.getMessage(),e);
		} catch (IOException e) {
			logger.error(e.getMessage(),e);
		}
		return xmlRep;
	}
	
	public static DomRepresentation getDomRepresentation(String code, long responseTime, 
			String comment, String[] attrNames, List<String[]> values, int totalSize) {
		DomRepresentation xmlRep = null;
		try {
			xmlRep = new DomRepresentation(MediaType.APPLICATION_XML);
			Document doc = xmlRep.getDocument();
			Element rootElement = doc.createElement(DomRepresentationUtil.ROOT_TAG);
			Element datasElement = doc.createElement(DomRepresentationUtil.DATA_ROOT_TAG);
			if(attrNames!=null&&values!=null){
				for(int i=0;i<values.size();i++){
					Element dataElement = doc.createElement(DomRepresentationUtil.DATA_TAG);
					String[] data=values.get(i);
					for(int j=0;j<data.length;j++){
						addElement(doc, dataElement, attrNames[j], data[j]);
					}
					datasElement.appendChild(dataElement);
				}
			}
			rootElement.appendChild(datasElement);
			addElement(doc, rootElement, DomRepresentationUtil.TOTAL_SIZE_TAG, String.valueOf(totalSize));
			addElement(doc, rootElement, DomRepresentationUtil.CODE_TAG, code);
			addElement(doc, rootElement, DomRepresentationUtil.RESPONSE_TIME_TAG, String.valueOf(responseTime));
			addElement(doc, rootElement, DomRepresentationUtil.COMMENT_TAG, comment);
			doc.appendChild(rootElement);
		} catch (DOMException e) {
			logger.error(e.getMessage(),e);
		} catch (IOException e) {
			logger.error(e.getMessage(),e);
		}
		return xmlRep;
	}
	
	private static void addElement(Document doc, Element parent, String name, String text){
		Element childElement = doc.createElement(name);
		childElement.setTextContent(text);
		parent.appendChild(childElement);
	}

}
